package com.glowdayz.glowmee;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.glowdayz.model.UserInfo;

public class CommonMenu {
	
	private static CommonMenu current = null;
	
	public static CommonMenu getInstance() {
		if (current == null) {
			current = new CommonMenu();
		}
		
		return current;
	}
	
	public static void freeInstance() {
		if (current != null) {
			current = null;
		}
	}
	
	public void setHeader(final Activity ac) {
		
		ImageView backBt = (ImageView) ac.findViewById(R.id.backBt);
		Button bottomBt1 = (Button) ac.findViewById(R.id.bottomBt1);
		Button bottomBt2 = (Button) ac.findViewById(R.id.bottomBt2);
		Button bottomBt3 = (Button) ac.findViewById(R.id.bottomBt3);
		Button bottomBt4 = (Button) ac.findViewById(R.id.bottomBt4);
		Button bottomBt5 = (Button) ac.findViewById(R.id.bottomBt5);
		
		if (backBt != null) {
			backBt.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					ac.finish();
				}
			});
		}

		bottomBt1.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (UserInfo.getInstance().getUSER_ID() == -2) {
					AlertDialog.Builder confirm = new AlertDialog.Builder(new ContextThemeWrapper(ac, android.R.style.Theme_Holo_Light_Dialog));
					confirm.setTitle("로그인 안내");
					confirm.setMessage("로그인 이후에 서비스 이용이 가능합니다.");
					confirm.setCancelable(false);
					confirm.setPositiveButton("확인", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent(ac, Login.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
							ac.startActivity(intent);
							return;
						}
					});
					confirm.create();
					confirm.show();
				} else {
					Intent i = new Intent(ac.getBaseContext(), ListPick.class);
					i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					ac.startActivity(i);
				}
			}
		});

		bottomBt2.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (UserInfo.getInstance().getUSER_ID() == -2) {
					AlertDialog.Builder confirm = new AlertDialog.Builder(new ContextThemeWrapper(ac, android.R.style.Theme_Holo_Light_Dialog));
					confirm.setTitle("로그인 안내");
					confirm.setMessage("로그인 이후에 서비스 이용이 가능합니다.");
					confirm.setCancelable(false);
					confirm.setPositiveButton("확인", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent(ac, Login.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
							ac.startActivity(intent);
							return;
						}
					});
					confirm.create();
					confirm.show();
				} else {
					Intent i = new Intent(ac.getBaseContext(), ListProduct.class);
					i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					ac.startActivity(i);
				}
			}
		});

		bottomBt3.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (UserInfo.getInstance().getUSER_ID() == -2) {
					AlertDialog.Builder confirm = new AlertDialog.Builder(new ContextThemeWrapper(ac, android.R.style.Theme_Holo_Light_Dialog));
					confirm.setTitle("로그인 안내");
					confirm.setMessage("로그인 이후에 서비스 이용이 가능합니다.");
					confirm.setCancelable(false);
					confirm.setPositiveButton("확인", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent(ac, Login.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
							ac.startActivity(intent);
							return;
						}
					});
					confirm.create();
					confirm.show();
				} else {
					Intent i = new Intent(ac.getBaseContext(), ListReview.class);
					i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					ac.startActivity(i);
				}
			}
		});

		bottomBt4.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (UserInfo.getInstance().getUSER_ID() == -2) {
					AlertDialog.Builder confirm = new AlertDialog.Builder(new ContextThemeWrapper(ac, android.R.style.Theme_Holo_Light_Dialog));
					confirm.setTitle("로그인 안내");
					confirm.setMessage("로그인 이후에 서비스 이용이 가능합니다.");
					confirm.setCancelable(false);
					confirm.setPositiveButton("확인", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent(ac, Login.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
							ac.startActivity(intent);
							return;
						}
					});
					confirm.create();
					confirm.show();
				} else {
					Intent i = new Intent(ac.getBaseContext(), ListEvent.class);
					i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
					ac.startActivity(i);
				}
			}
		});

		bottomBt5.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (UserInfo.getInstance().getUSER_ID() == -2) {
					AlertDialog.Builder confirm = new AlertDialog.Builder(new ContextThemeWrapper(ac, android.R.style.Theme_Holo_Light_Dialog));
					confirm.setTitle("로그인 안내");
					confirm.setMessage("로그인 이후에 서비스 이용이 가능합니다.");
					confirm.setCancelable(false);
					confirm.setPositiveButton("확인", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent(ac, Login.class);
							intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
							ac.startActivity(intent);
							return;
						}
					});
					confirm.create();
					confirm.show();
				} else {
					
					Intent i = new Intent(ac.getBaseContext(), More.class);
					i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
					ac.startActivity(i);
					
					/*View popupView = View.inflate(ac, R.layout.inc_more, null);
					
					ListView listMore = null;
					ArrayList<String> list = new ArrayList<String>();
					list.add("내 프로필");
					list.add("설정");
					list.add("공지사항");
					list.add("친구 초대하기");
					ArrayAdapter<String> adapter = new ArrayAdapter<String>(ac, R.layout.inc_more_text, list);
					listMore = (ListView) popupView.findViewById(R.id.listMore);
					listMore.setAdapter(adapter);
					listMore.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
					
					final Dialog dialog = new Dialog(ac);
					dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
					
					dialog.setContentView(popupView);
					dialog.show();
					
					WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
					lp.dimAmount = 0.7f;
					lp.x = 300;
					lp.y = 320;
					lp.width = 350;
					lp.height = 421;
					dialog.getWindow().setAttributes(lp);
					
					listMore.setOnItemClickListener(new OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {
							dialog.hide();
							switch (arg2) {
							case 0:
								Intent intent0 = new Intent(ac, ProfileHome.class);
								ac.startActivity(intent0);
								break;
							case 1:
								Intent intent1 = new Intent(ac, Setup.class);
								ac.startActivity(intent1);
								break;
							case 2:
								Intent intent2 = new Intent(ac, Notice.class);
								ac.startActivity(intent2);
								break;
							case 3:
								setReady(ac);
								break;
							}
						}
					});*/
				}
			}
		});
	}
	
	public void setReady(final Activity ac) {
		AlertDialog.Builder temp = new AlertDialog.Builder(new ContextThemeWrapper(ac, android.R.style.Theme_Holo_Light_Dialog));
		temp.setMessage("준비중입니다.");
		temp.setCancelable(false);
		temp.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		temp.create();
		temp.show();
	}
}
